package com.embedsky.administrator.mycardreader.p2p;

import static android.content.Context.WIFI_P2P_SERVICE;

import android.annotation.SuppressLint;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.embedsky.administrator.mycardreader.MyApplication;


/**
 * @Des:
 * @Author: hang.zhao
 * @Date: 2023/2/26
 */
@SuppressLint("MissingPermission")
public class P2pManager {


    private String TAG = P2pManager.class.getSimpleName();

    private  WifiP2pManager wifiP2pManager = (WifiP2pManager) MyApplication.getContext().getSystemService(WIFI_P2P_SERVICE);

    private  WifiP2pManager.Channel channel = wifiP2pManager.initialize(MyApplication.getContext(), Looper.myLooper(), () -> {

    });

    public static P2pManager getInstance() {
        return Inner.INSTANCE;
    }

    private static class Inner {
        private static P2pManager INSTANCE = new P2pManager();
    }

    private P2pManager() {
    }

    public void createGroup() {
        wifiP2pManager.createGroup(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "create group onSuccess: ");
            }

            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "create group fail,the reason is: " + reason);
            }
        });
    }

    public void removeGroup(WifiP2pManager wifiP2pManager, WifiP2pManager.Channel channel) {
        wifiP2pManager.removeGroup(channel, new WifiP2pManager.ActionListener() {

            @Override
            public void onSuccess() {
                Log.d(TAG, "remove group onSuccess: ");
            }

            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "remove group fail,the reason is: " + reason);
            }
        });
    }

    public void discoverPeers(WifiP2pManager wifiP2pManager, WifiP2pManager.Channel channel) {
        wifiP2pManager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "discover peers onSuccess: ");
            }

            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "discover peers fail,the reason is: " + reason);
            }
        });

    }

    public void connect(WifiP2pManager wifiP2pManager, WifiP2pConfig config, WifiP2pManager.Channel channel) {
        wifiP2pManager.connect(channel, config, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "connect onSuccess: ");
                Toast.makeText(MyApplication.getContext(), "p2p连接成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "connect fail,the reason is: " + reason);
            }
        });
    }

    public void disconnect(WifiP2pManager wifiP2pManager, WifiP2pManager.Channel channel) {
        removeGroup(wifiP2pManager,channel);
        wifiP2pManager.cancelConnect(channel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.d(TAG, "disconnect onSuccess: ");
            }

            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "disconnect fail,the reason is: " + reason);
            }
        });
    }

}
